<html>
<head>
  <title>Evernote Export</title>
  <basefont face="微软雅黑" size="2" />
  <meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
  <meta name="exporter-version" content="YXBJ Windows/600718 (zh-CN, DDL); Windows/10.0.0 (Win64);"/>
  <meta name="content-class" content="yinxiang.markdown"/>
  <style>
    body, td {
      font-family: 微软雅黑;
      font-size: 10pt;
    }
  </style>
</head>
<body>
<a name="855"/>

<div><span><div style="font-size: 14px; margin: 0; padding: 0; width: 100%;"><p style="line-height: 160%; box-sizing: content-box; margin: 10px 0; color: #333;">场景：</p>
<ol style="line-height: 160%; box-sizing: content-box; display: block; padding-left: 30px; margin: 6px 0 10px; color: #333; list-style-type: decimal;">
<li style="line-height: 160%; box-sizing: content-box;">集中管理，一次修改处处生效</li>
<li style="line-height: 160%; box-sizing: content-box;">不同环境不同配置，开发线上调试环境</li>
<li style="line-height: 160%; box-sizing: content-box;">允许运行期间动态修改，数据库连接池的大小等信息</li>
<li style="line-height: 160%; box-sizing: content-box;">配置内容变化，微服务自动更新配置<br/>
解决方案：Spring Cloud Config<br/>
结构图<br/>
<img src="05 Spring Cloud Config 分布式配置中心_files/Image.png" type="image/png" data-filename="Image.png"/><br/>
Config Server是集中式的配置服务，⽤于集中管理应⽤程序各个环境下的配置。 默认使⽤Git存储配置⽂件内容，也可以SVN。⽐如，我们要对“简历微服务”的application.yml进⾏管理（区分开发环境、测试环境、⽣产环境）<br/>
1）登录码云，创建项⽬lagou-config-repo<br/>
2）上传yml配置⽂件，命名规则如下：<br/>
{application}-{profile}.yml 或者 {application}-{profile}.properties<br/>
其中，application为应⽤名称，profile指的是环境（⽤于区分开发环境，测试环<br/>
境、⽣产环境等）<br/>
示例：<br/>
lagou-service-resume-dev.yml、<br/>
lagou-service-resume-test.yml、<br/>
lagou-service-resume-prod.yml</li>
</ol>
<p style="line-height: 160%; box-sizing: content-box; margin: 10px 0; color: #333;">使用<br/>
1.引入依赖<br/>
2.配置修改<br/>
3.启动类增加注解<br/>
4.微服务子件修改增加依赖</p>
<p style="line-height: 160%; box-sizing: content-box; margin: 10px 0; color: #333;">手动刷新<br/>
⼿动刷新⽅式避免了服务重启（流程：Git改配置—&gt;for循环脚本⼿动刷新每个微服务）</p>
<p style="line-height: 160%; box-sizing: content-box; margin: 10px 0; color: #333;">1）Client客户端添加依赖springboot-starter-actuator（已添加）<br/>
2）Client客户端bootstrap.yml中添加配置（暴露通信端点）</p>
<pre style="line-height: 160%; box-sizing: content-box; border: 0; border-radius: 0; margin: 2px 0 8px; background-color: #f5f7f8;"><code style="display: block; overflow-x: auto; background: #1e1e1e; line-height: 160%; box-sizing: content-box; border: 0; border-radius: 0; letter-spacing: -.3px; padding: 18px; color: #f4f4f4; white-space: pre-wrap;">management:
    endpoints:
        web:
            exposure:
                include: refresh
也可以暴露所有的端⼝
management:
    endpoints:
        web:
            exposure:
                include: &quot;*&quot;
</code></pre>
<p style="line-height: 160%; box-sizing: content-box; margin: 10px 0; color: #333;">3）Client客户端使⽤到配置信息的类上添加@RefreshScope<br/>
4）⼿动向Client客户端发起POST请求，http://localhost:8080/actuator/refresh，</p>
<p style="line-height: 160%; box-sizing: content-box; margin: 10px 0; color: #333;">刷新配置信息思考：<br/>
手动缺点：每个微服务都要修改配置，并且发送一次post更新请求，可以使用脚本循环<br/>
可否使⽤⼴播机制，⼀次通知，处处⽣效，⽅便⼤范围配置刷新？</p>
<p style="line-height: 160%; box-sizing: content-box; margin: 10px 0; color: #333;">自动刷新<br/>
微服务架构中，我们可以结合消息总线（Bus）实现分布式配置的⾃动更新<br/>
（Spring Cloud Config+Spring Cloud Bus）<br/>
<strong style="line-height: 160%; box-sizing: content-box; font-weight: 700;">消息总线Bus</strong><br/>
所谓消息总线Bus，即我们经常会使⽤MQ消息代理构建⼀个共⽤的Topic，通过这个<br/>
Topic连接各个微服务实例，MQ⼴播的消息会被所有在注册中⼼的微服务实例监听<br/>
和消费。换⾔之就是通过⼀个主题连接各个微服务，打通脉络。<br/>
Spring Cloud Bus（基于MQ的，⽀持RabbitMq/Kafka） 是Spring Cloud中的消息<br/>
总线⽅案，Spring Cloud Config + Spring Cloud Bus 结合可以实现配置信息的⾃动更新。</p>
<p style="line-height: 160%; box-sizing: content-box; margin: 10px 0; color: #333;">重启各个服务，更改配置之后，向配置中⼼服务端发送post请求<br/>
http://localhost:9003/actuator/bus-refresh，各个客户端配置即可⾃动刷新</p>
</div><center style="display:none !important;visibility:collapse !important;height:0 !important;white-space:nowrap;width:100%;overflow:hidden">%E5%9C%BA%E6%99%AF%EF%BC%9A%0A1.%20%E9%9B%86%E4%B8%AD%E7%AE%A1%E7%90%86%EF%BC%8C%E4%B8%80%E6%AC%A1%E4%BF%AE%E6%94%B9%E5%A4%84%E5%A4%84%E7%94%9F%E6%95%88%0A2.%20%E4%B8%8D%E5%90%8C%E7%8E%AF%E5%A2%83%E4%B8%8D%E5%90%8C%E9%85%8D%E7%BD%AE%EF%BC%8C%E5%BC%80%E5%8F%91%E7%BA%BF%E4%B8%8A%E8%B0%83%E8%AF%95%E7%8E%AF%E5%A2%83%0A3.%20%E5%85%81%E8%AE%B8%E8%BF%90%E8%A1%8C%E6%9C%9F%E9%97%B4%E5%8A%A8%E6%80%81%E4%BF%AE%E6%94%B9%EF%BC%8C%E6%95%B0%E6%8D%AE%E5%BA%93%E8%BF%9E%E6%8E%A5%E6%B1%A0%E7%9A%84%E5%A4%A7%E5%B0%8F%E7%AD%89%E4%BF%A1%E6%81%AF%0A4.%20%E9%85%8D%E7%BD%AE%E5%86%85%E5%AE%B9%E5%8F%98%E5%8C%96%EF%BC%8C%E5%BE%AE%E6%9C%8D%E5%8A%A1%E8%87%AA%E5%8A%A8%E6%9B%B4%E6%96%B0%E9%85%8D%E7%BD%AE%0A%E8%A7%A3%E5%86%B3%E6%96%B9%E6%A1%88%EF%BC%9ASpring%20Cloud%20Config%0A%E7%BB%93%E6%9E%84%E5%9B%BE%0A!%5B7f5663c042303e2fd08887f78c0d0771.png%5D(en-resource%3A%2F%2Fdatabase%2F857%3A0)%0AConfig%20Server%E6%98%AF%E9%9B%86%E4%B8%AD%E5%BC%8F%E7%9A%84%E9%85%8D%E7%BD%AE%E6%9C%8D%E5%8A%A1%EF%BC%8C%E2%BD%A4%E4%BA%8E%E9%9B%86%E4%B8%AD%E7%AE%A1%E7%90%86%E5%BA%94%E2%BD%A4%E7%A8%8B%E5%BA%8F%E5%90%84%E4%B8%AA%E7%8E%AF%E5%A2%83%E4%B8%8B%E7%9A%84%E9%85%8D%E7%BD%AE%E3%80%82%20%E9%BB%98%E8%AE%A4%E4%BD%BF%E2%BD%A4Git%E5%AD%98%E5%82%A8%E9%85%8D%E7%BD%AE%E2%BD%82%E4%BB%B6%E5%86%85%E5%AE%B9%EF%BC%8C%E4%B9%9F%E5%8F%AF%E4%BB%A5SVN%E3%80%82%E2%BD%90%E5%A6%82%EF%BC%8C%E6%88%91%E4%BB%AC%E8%A6%81%E5%AF%B9%E2%80%9C%E7%AE%80%E5%8E%86%E5%BE%AE%E6%9C%8D%E5%8A%A1%E2%80%9D%E7%9A%84application.yml%E8%BF%9B%E2%BE%8F%E7%AE%A1%E7%90%86%EF%BC%88%E5%8C%BA%E5%88%86%E5%BC%80%E5%8F%91%E7%8E%AF%E5%A2%83%E3%80%81%E6%B5%8B%E8%AF%95%E7%8E%AF%E5%A2%83%E3%80%81%E2%BD%A3%E4%BA%A7%E7%8E%AF%E5%A2%83%EF%BC%89%0A1%EF%BC%89%E7%99%BB%E5%BD%95%E7%A0%81%E4%BA%91%EF%BC%8C%E5%88%9B%E5%BB%BA%E9%A1%B9%E2%BD%AClagou-config-repo%0A2%EF%BC%89%E4%B8%8A%E4%BC%A0yml%E9%85%8D%E7%BD%AE%E2%BD%82%E4%BB%B6%EF%BC%8C%E5%91%BD%E5%90%8D%E8%A7%84%E5%88%99%E5%A6%82%E4%B8%8B%EF%BC%9A%0A%7Bapplication%7D-%7Bprofile%7D.yml%20%E6%88%96%E8%80%85%20%7Bapplication%7D-%7Bprofile%7D.properties%0A%E5%85%B6%E4%B8%AD%EF%BC%8Capplication%E4%B8%BA%E5%BA%94%E2%BD%A4%E5%90%8D%E7%A7%B0%EF%BC%8Cprofile%E6%8C%87%E7%9A%84%E6%98%AF%E7%8E%AF%E5%A2%83%EF%BC%88%E2%BD%A4%E4%BA%8E%E5%8C%BA%E5%88%86%E5%BC%80%E5%8F%91%E7%8E%AF%E5%A2%83%EF%BC%8C%E6%B5%8B%E8%AF%95%E7%8E%AF%0A%E5%A2%83%E3%80%81%E2%BD%A3%E4%BA%A7%E7%8E%AF%E5%A2%83%E7%AD%89%EF%BC%89%0A%E7%A4%BA%E4%BE%8B%EF%BC%9A%0Alagou-service-resume-dev.yml%E3%80%81%0Alagou-service-resume-test.yml%E3%80%81%0Alagou-service-resume-prod.yml%0A%0A%E4%BD%BF%E7%94%A8%0A1.%E5%BC%95%E5%85%A5%E4%BE%9D%E8%B5%96%0A2.%E9%85%8D%E7%BD%AE%E4%BF%AE%E6%94%B9%0A3.%E5%90%AF%E5%8A%A8%E7%B1%BB%E5%A2%9E%E5%8A%A0%E6%B3%A8%E8%A7%A3%0A4.%E5%BE%AE%E6%9C%8D%E5%8A%A1%E5%AD%90%E4%BB%B6%E4%BF%AE%E6%94%B9%E5%A2%9E%E5%8A%A0%E4%BE%9D%E8%B5%96%0A%0A%0A%0A%E6%89%8B%E5%8A%A8%E5%88%B7%E6%96%B0%20%0A%E2%BC%BF%E5%8A%A8%E5%88%B7%E6%96%B0%E2%BD%85%E5%BC%8F%E9%81%BF%E5%85%8D%E4%BA%86%E6%9C%8D%E5%8A%A1%E9%87%8D%E5%90%AF%EF%BC%88%E6%B5%81%E7%A8%8B%EF%BC%9AGit%E6%94%B9%E9%85%8D%E7%BD%AE%E2%80%94%3Efor%E5%BE%AA%E7%8E%AF%E8%84%9A%E6%9C%AC%E2%BC%BF%E5%8A%A8%E5%88%B7%E6%96%B0%E6%AF%8F%E4%B8%AA%E5%BE%AE%E6%9C%8D%E5%8A%A1%EF%BC%89%0A%0A1%EF%BC%89Client%E5%AE%A2%E6%88%B7%E7%AB%AF%E6%B7%BB%E5%8A%A0%E4%BE%9D%E8%B5%96springboot-starter-actuator%EF%BC%88%E5%B7%B2%E6%B7%BB%E5%8A%A0%EF%BC%89%0A2%EF%BC%89Client%E5%AE%A2%E6%88%B7%E7%AB%AFbootstrap.yml%E4%B8%AD%E6%B7%BB%E5%8A%A0%E9%85%8D%E7%BD%AE%EF%BC%88%E6%9A%B4%E9%9C%B2%E9%80%9A%E4%BF%A1%E7%AB%AF%E7%82%B9%EF%BC%89%0A%60%60%60%0Amanagement%3A%0A%20%20%20%20endpoints%3A%0A%20%20%20%20%20%20%20%20web%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20exposure%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20include%3A%20refresh%0A%E4%B9%9F%E5%8F%AF%E4%BB%A5%E6%9A%B4%E9%9C%B2%E6%89%80%E6%9C%89%E7%9A%84%E7%AB%AF%E2%BC%9D%0Amanagement%3A%0A%20%20%20%20endpoints%3A%0A%20%20%20%20%20%20%20%20web%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20exposure%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20include%3A%20%22*%22%0A%60%60%60%0A3%EF%BC%89Client%E5%AE%A2%E6%88%B7%E7%AB%AF%E4%BD%BF%E2%BD%A4%E5%88%B0%E9%85%8D%E7%BD%AE%E4%BF%A1%E6%81%AF%E7%9A%84%E7%B1%BB%E4%B8%8A%E6%B7%BB%E5%8A%A0%40RefreshScope%0A4%EF%BC%89%E2%BC%BF%E5%8A%A8%E5%90%91Client%E5%AE%A2%E6%88%B7%E7%AB%AF%E5%8F%91%E8%B5%B7POST%E8%AF%B7%E6%B1%82%EF%BC%8Chttp%3A%2F%2Flocalhost%3A8080%2Factuator%2Frefresh%EF%BC%8C%0A%0A%E5%88%B7%E6%96%B0%E9%85%8D%E7%BD%AE%E4%BF%A1%E6%81%AF%E6%80%9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0%E9%85%8D%E7%BD%AE%E4%BF%A1%E6%81%AF%E7%9A%84%E2%BE%83%E5%8A%A8%E6%9B%B4%E6%96%B0%E3%80%82%0A%0A%E9%87%8D%E5%90%AF%E5%90%84%E4%B8%AA%E6%9C%8D%E5%8A%A1%EF%BC%8C%E6%9B%B4%E6%94%B9%E9%85%8D%E7%BD%AE%E4%B9%8B%E5%90%8E%EF%BC%8C%E5%90%91%E9%85%8D%E7%BD%AE%E4%B8%AD%E2%BC%BC%E6%9C%8D%E5%8A%A1%E7%AB%AF%E5%8F%91%E9%80%81post%E8%AF%B7%E6%B1%82%0Ahttp%3A%2F%2Flocalhost%3A9003%2Factuator%2Fbus-refresh%EF%BC%8C%E5%90%84%E4%B8%AA%E5%AE%A2%E6%88%B7%E7%AB%AF%E9%85%8D%E7%BD%AE%E5%8D%B3%E5%8F%AF%E2%BE%83%E5%8A%A8%E5%88%B7%E6%96%B0%0A%0A%E5%9C%A8%E2%BC%B4%E6%92%AD%E6%A8%A1%E5%BC%8F%E4%B8%8B%E5%AE%9E%E7%8E%B0%E4%BA%86%E2%BC%80%E6%AC%A1%E8%AF%B7%E6%B1%82%EF%BC%8C%E5%A4%84%E5%A4%84%E6%9B%B4%E6%96%B0%EF%BC%8C%E5%A6%82%E6%9E%9C%E6%88%91%E5%8F%AA%E6%83%B3%E5%AE%9A%E5%90%91%E6%9B%B4%E6%96%B0%E5%91%A2%EF%BC%9F%0A%E5%9C%A8%E5%8F%91%E8%B5%B7%E5%88%B7%E6%96%B0%E8%AF%B7%E6%B1%82%E7%9A%84%E6%97%B6%E5%80%99http%3A%2F%2Flocalhost%3A9006%2Factuator%2Fbus-refresh%2Flagou-service-%0Aresume%3A8081%0A%E5%8D%B3%E4%B8%BA%E6%9C%80%E5%90%8E%E2%BE%AF%E8%B7%9F%E4%B8%8A%E8%A6%81%E5%AE%9A%E5%90%91%E5%88%B7%E6%96%B0%E7%9A%84%E5%AE%9E%E4%BE%8B%E7%9A%84%20%E6%9C%8D%E5%8A%A1%E5%90%8D%3A%E7%AB%AF%E2%BC%9D%E5%8F%B7%E5%8D%B3%E5%8F%AF%0A</center></span>
</div></body></html> 